package com.yzjs.localhost.service.hrmEducationInfo.impl;

import com.alibaba.fastjson.JSONObject;
import com.yzjs.common.base.service.BaseServiceImpl;
import com.yzjs.common.util.conversion.JsonUtil;
import com.yzjs.localhost.dto.ComparisonResultDTO;
import com.yzjs.localhost.dto.DataBundlingDTO;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.hrmEducationInfo.HrmEducationInfoEntity;
import com.yzjs.localhost.dao.hrmEducationInfo.HrmEducationInfoMapper;
import com.yzjs.localhost.entity.tDataPersistenceAfter.TDataPersistenceAfterEntity;
import com.yzjs.localhost.entity.tDataPersistenceBeforce.TDataPersistenceBeforceEntity;
import com.yzjs.localhost.service.AnalysisService;
import com.yzjs.localhost.service.hrmEducationInfo.HrmEducationInfoService;
import org.springframework.stereotype.Service;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("hrmEducationInfoService")
public class HrmEducationInfoServiceImpl extends BaseServiceImpl<HrmEducationInfoEntity, HrmEducationInfoMapper> implements HrmEducationInfoService {

    @Override
    public ComparisonResultDTO hrmEducationInfoCompliance(DataBundlingDTO dto, UserRightsDTO rights) throws Exception {
        TDataPersistenceBeforceEntity before = dto.getBeforce();
        String beforeInfo = before.getInfo();
        Map<String, Object> beforeParse = JsonUtil.json2map(beforeInfo);
        TDataPersistenceAfterEntity after = dto.getAfter();
        String afterInfo = after.getInfo();
        ComparisonResultDTO resultDTO = null;
        Map<String, Object> afterParse = JsonUtil.json2map(afterInfo);
        if (before.getOperatorCode().contains("education:add")) {
            // 比对 用户ID,创建人，院校，主修课程，删除标记，教育培训时间，专业，备注
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse, "eduUserId,createBy,collegesName,majorCourses,delFlag,eduDate,majorName,remarks");
        } else if (before.getOperatorCode().contains("education:edit")) {
            // 比对 用户ID，院校，主修课程，删除标记，教育培训时间，主键ID，创建人，修改人，专业，备注，创建时间
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse, "eduUserId,collegesName,majorCourses,delFlag,eduDate,eduIds,createBy,updateBy,majorName,remarks,createDate");
        } else if (before.getOperatorCode().contains("education:remove")) {
            beforeParse.put("delFlag", "2");
            // 比对 主键ID
            resultDTO = AnalysisService.comparisonMap(before.getOperatorCode(), beforeParse, afterParse, "eduIds,delFlag");
            if (null==afterParse.get("eduIds")){
                resultDTO.setAbstractName("主键ID为空");
                resultDTO.setWarningInfo("主键ID为空");
                resultDTO.setMathFlag(false);
                return resultDTO;
            }
            HrmEducationInfoEntity hrmEducationInfoEntity = new HrmEducationInfoEntity();
            hrmEducationInfoEntity.setEduId(Long.valueOf((String) afterParse.get("eduIds")));
            hrmEducationInfoEntity.setDelFlag((String) afterParse.get("delFlag"));
            this.insert(hrmEducationInfoEntity);
            return resultDTO;
        }
        // 记录表信息
        HrmEducationInfoEntity hrmEducationInfoEntity = JSONObject.parseObject(after.getInfo(), HrmEducationInfoEntity.class);
        hrmEducationInfoEntity.setPermissionMirroring(JsonUtil.obj2jsonIgnoreNull(rights));
        hrmEducationInfoEntity.setDataIdentification(after.getDataIdentification());
        this.insert(hrmEducationInfoEntity);
        return resultDTO;
    }
}